/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MrpMrp;

import merlion.mrp.ppm.pojo.LongHolder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import managedbean.Workspace.WorkspaceManagerBean;
import merlion.common.util.Consts;
import merlion.mrp.ppm.entity.IndexNValue;
import merlion.mrp.ppm.entity.MRP;
import merlion.mrp.ppm.session.MRPSessionLocal;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class MrpMrpManagerBean {

    public static final String NEW_MRP = "newMrp";
    @EJB
    private MRPSessionLocal mrpSession;
    @ManagedProperty(value = "#{workspaceManagerBean}")
    private WorkspaceManagerBean workspaceManagerBean;
    // for select page
    private Integer selectedYear;
    private String selectedMonth;
    private List<Integer> yearList;
    private List<String> monthList;
    private ArrayList<Integer> counterForProduct;
    private ArrayList<Integer> counterForWeek;
    // for create page - dialog
    private MRP selectedMRP;
    private ArrayList<ArrayList<Long>> weeklyDemands;
    private ArrayList<Long> plannedReceiptsList; // size : 5
    private ArrayList<LongHolder> inputLotNumbers; // size : 5
    private ArrayList<Long> plannedInventory; // size : 6
    private ArrayList<MRP> newMRPs;
    private Integer numOfWks;

    /** Creates a new instance of MrpMrpManagerBean */
    public MrpMrpManagerBean() {
        counterForProduct = new ArrayList<Integer>();
        for (int i = 0; i < 4; i++) {
            counterForProduct.add(i);
        }
        counterForWeek = new ArrayList<Integer>();
        for (int i = 0; i < 5; i++) {
            counterForWeek.add(i);
        }
        weeklyDemands = new ArrayList<ArrayList<Long>>();
        inputLotNumbers = new ArrayList<LongHolder>();
        plannedInventory = new ArrayList<Long>();
        plannedReceiptsList = new ArrayList<Long>();
        // init
        for (int i = 0; i < 6; i++) {
            plannedInventory.add(0L);
        }
        for (int i = 0; i < 5; i++) {
            LongHolder temp = new LongHolder();
            temp.setL(0L);
            inputLotNumbers.add(temp);
            plannedReceiptsList.add(0L);
        }
        for (int i = 0; i < 4; i++) {
            ArrayList<Long> temp = new ArrayList<Long>();
            for (int j = 0; j < 5; j++) {
                temp.add(0L);
            }
            weeklyDemands.add(temp);
        }
    }

    // for select page
    public String redirectToCreateMRPDetail() {
        int month = getMonthIndex(selectedMonth);
        if (!mrpSession.checkAbleToCreateMRP(month, selectedYear)) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful",
                    "Sorry, you are not allowed to create this period's mrp because the weekly mps is not ready."));
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        ArrayList<MRP> mrplist = new ArrayList<MRP>();
        int counter = 0;
        for (String materialName : Consts.allRawMaterialNames) {
            MRP nmrp = new MRP(selectedYear, month, new Timestamp(date.getTime()), counter);
            nmrp.setBom(mrpSession.getBOM(materialName));
            mrplist.add(nmrp);
            counter++;
        }

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(NEW_MRP);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(NEW_MRP, mrplist);
        return getRedirectPath(Consts.CREATE_MRP_MRP_PATH + "Detail");
    }

    public List<String> getMonthList() {
        if (monthList == null) {
            monthList = new ArrayList<String>();
            for (int i = 1; i <= 12; i++) {
                monthList.add(getMonthEnglish(i));
            }
        }
        return monthList;
    }

    public void setMonthList(List<String> monthList) {
        this.monthList = monthList;
    }

    public List<Integer> getYearList() {
        if (yearList == null) {
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            int currentYear = Integer.valueOf(Consts.sdfYear.format(date));
            yearList = new ArrayList<Integer>();
            for (int i = currentYear; i < (currentYear + 5); i++) {
                yearList.add(i);
            }
        }
        return yearList;
    }

    public void setYearList(List<Integer> yearList) {
        this.yearList = yearList;
    }

    public String getSelectedMonth() {
        return selectedMonth;
    }

    public void setSelectedMonth(String selectedMonth) {
        this.selectedMonth = selectedMonth;
    }

    public Integer getSelectedYear() {
        return selectedYear;
    }

    public void setSelectedYear(Integer selectedYear) {
        this.selectedYear = selectedYear;
    }

    // for create detail page
    public void autoOptimizationIndiv(ActionEvent event) {
        for (int i = 0; i < numOfWks; i++) {
            Long inv = selectedMRP.getPlannedInventory().get(i).getLongValue();
            if (inv < 0) {
                inv = Math.abs(inv);
                Long numOfLots;
                if (inv % selectedMRP.getBom().getMaterial().getLotSize() == 0) {
                    numOfLots = inv / selectedMRP.getBom().getMaterial().getLotSize();
                } else {
                    numOfLots = inv / selectedMRP.getBom().getMaterial().getLotSize() + 1;
                }
                Long prevRec = selectedMRP.getPlannedReceipts().get(i).getLongValue();
                Long currentPlannedReceipt = prevRec + numOfLots * selectedMRP.getBom().getMaterial().getLotSize();
                selectedMRP.getPlannedReceipts().get(i).setLongValue(currentPlannedReceipt);
                inputLotNumbers.get(i).setL(numOfLots + prevRec / selectedMRP.getBom().getMaterial().getLotSize());
                plannedReceiptsList.set(i, currentPlannedReceipt);
                for (int j = i; j < numOfWks; j++) {
                    Long prevInv;
                    if (j == 0) {
                        prevInv = mrpSession.getPlannedInventoryForPrevWeek(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth(), selectedMRP.getBom());
                    } else {
                        prevInv = selectedMRP.getPlannedInventory().get(j - 1).getLongValue();
                    }
                    Long currentInv = prevInv + selectedMRP.getPlannedReceipts().get(j).getLongValue() - selectedMRP.getGrossRequirements().get(j).getLongValue();
                    selectedMRP.getPlannedInventory().get(j).setLongValue(currentInv);
                }
            }
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful",
                "Filled in for this material done."));
    }

    public void autoOptimizationIndivWSafetyStock(ActionEvent event) {
        for (int i = 0; i < numOfWks; i++) {
            Long inv = selectedMRP.getPlannedInventory().get(i).getLongValue();
            // need to get safety stock from scm
            Long safteStock = mrpSession.getRawMaterialSafetyStockFromSCM(selectedMRP.getBom().getMaterial().getName());
            inv = inv - safteStock;
            if (inv < 0) {
                inv = Math.abs(inv);
                Long numOfLots;
                if (inv % selectedMRP.getBom().getMaterial().getLotSize() == 0) {
                    numOfLots = inv / selectedMRP.getBom().getMaterial().getLotSize();
                } else {
                    numOfLots = inv / selectedMRP.getBom().getMaterial().getLotSize() + 1;
                }
                Long prevRec = selectedMRP.getPlannedReceipts().get(i).getLongValue();
                Long currentPlannedReceipt = prevRec + numOfLots * selectedMRP.getBom().getMaterial().getLotSize();
                selectedMRP.getPlannedReceipts().get(i).setLongValue(currentPlannedReceipt);
                inputLotNumbers.get(i).setL(numOfLots + prevRec / selectedMRP.getBom().getMaterial().getLotSize());
                plannedReceiptsList.set(i, currentPlannedReceipt);
                for (int j = i; j < numOfWks; j++) {
                    Long prevInv;
                    if (j == 0) {
                        prevInv = mrpSession.getPlannedInventoryForPrevWeek(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth(), selectedMRP.getBom());
                    } else {
                        prevInv = selectedMRP.getPlannedInventory().get(j - 1).getLongValue();
                    }
                    Long currentInv = prevInv + selectedMRP.getPlannedReceipts().get(j).getLongValue() - selectedMRP.getGrossRequirements().get(j).getLongValue();
                    selectedMRP.getPlannedInventory().get(j).setLongValue(currentInv);
                }
            }
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful",
                "Optimization for this material done."));
    }

    public void autoOptimization(ActionEvent event) {
        // to compute all the negative ones with algorithm
        for (MRP m : newMRPs) {
            for (int i = 0; i < getNumOfWks(); i++) {
                Long inv = m.getPlannedInventory().get(i).getLongValue();
                if (inv < 0) {
                    inv = Math.abs(inv);
                    Long numOfLots;
                    if (inv % m.getBom().getMaterial().getLotSize() == 0) {
                        numOfLots = inv / m.getBom().getMaterial().getLotSize();
                    } else {
                        numOfLots = inv / m.getBom().getMaterial().getLotSize() + 1;
                    }
                    Long prevRec = m.getPlannedReceipts().get(i).getLongValue();
                    Long currentPlannedReceipt = prevRec + numOfLots * m.getBom().getMaterial().getLotSize();
                    m.getPlannedReceipts().get(i).setLongValue(currentPlannedReceipt);
//                    inputLotNumbers.get(i).setL(numOfLots);
//                    plannedReceiptsList.set(i, currentPlannedReceipt);

                    for (int j = i; j < numOfWks; j++) {
                        Long prevInv;
                        if (j == 0) {
                            prevInv = mrpSession.getPlannedInventoryForPrevWeek(m.getStartPeriodYear(), m.getStartPeriodMonth(), m.getBom());
                        } else {
                            prevInv = m.getPlannedInventory().get(j - 1).getLongValue();
                        }
                        Long currentInv = prevInv + m.getPlannedReceipts().get(j).getLongValue() - m.getGrossRequirements().get(j).getLongValue();
                        m.getPlannedInventory().get(j).setLongValue(currentInv);
                    }
                }
            }
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful",
                "Filled in for all materials done."));
    }

    public void autoOptimizationWSafetyStock(ActionEvent event) {
        // to compute all the negative ones with algorithm
        for (MRP m : newMRPs) {
            for (int i = 0; i < getNumOfWks(); i++) {
                Long inv = m.getPlannedInventory().get(i).getLongValue();
                //  need to get safety stock from scm
                Long safteStock = mrpSession.getRawMaterialSafetyStockFromSCM(m.getBom().getMaterial().getName());
                inv = inv - safteStock;
                if (inv < 0) {
                    inv = Math.abs(inv);
                    Long numOfLots;
                    if (inv % m.getBom().getMaterial().getLotSize() == 0) {
                        numOfLots = inv / m.getBom().getMaterial().getLotSize();
                    } else {
                        numOfLots = inv / m.getBom().getMaterial().getLotSize() + 1;
                    }
                    Long currentPlannedReceipt = m.getPlannedReceipts().get(i).getLongValue() + numOfLots * m.getBom().getMaterial().getLotSize();
                    m.getPlannedReceipts().get(i).setLongValue(currentPlannedReceipt);
//                    inputLotNumbers.get(i).setL(numOfLots);
//                    plannedReceiptsList.set(i, currentPlannedReceipt);

                    for (int j = i; j < numOfWks; j++) {
                        Long prevInv;
                        if (j == 0) {
                            prevInv = mrpSession.getPlannedInventoryForPrevWeek(m.getStartPeriodYear(), m.getStartPeriodMonth(), m.getBom());
                        } else {
                            prevInv = m.getPlannedInventory().get(j - 1).getLongValue();
                        }
                        Long currentInv = prevInv + m.getPlannedReceipts().get(j).getLongValue() - m.getGrossRequirements().get(j).getLongValue();
                        m.getPlannedInventory().get(j).setLongValue(currentInv);
                    }
                }
            }
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful",
                "Optimization in for all materials done."));
    }

    public void calculateMRP(ActionEvent event) {
        RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.addCallbackParam("isValid", true);
        // substitute the onhand current value and planned receipts
        for (int i = 0; i < getNumOfWks(); i++) {
            // set planned receipts
            plannedReceiptsList.set(i, inputLotNumbers.get(i).getL() * selectedMRP.getBom().getMaterial().getLotSize());
            selectedMRP.getPlannedReceipts().get(i).setLongValue(plannedReceiptsList.get(i));
            // set planned inventory
            Long diff = plannedInventory.get(i) + plannedReceiptsList.get(i) - selectedMRP.getGrossRequirements().get(i).getLongValue();
            selectedMRP.getPlannedInventory().get(i).setLongValue(diff);
            plannedInventory.set(i + 1, diff);
        }
        // check whether it is not negative
        for (int i = 0; i < getNumOfWks(); i++) {
            if (selectedMRP.getGrossRequirements().get(i).getLongValue() < 0
                    || selectedMRP.getPlannedInventory().get(i).getLongValue() < 0
                    || selectedMRP.getPlannedReceipts().get(i).getLongValue() < 0) {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful",
                        "Sorry. On hand inventory cannot be negative. Please check week" + (i + 1)));
                requestContext.addCallbackParam("isValid", false);
            }
        }
    }

    public String confirmMrp() {
        // check whether all the planned receipts is 0
        for (MRP m : newMRPs) {
            for (int i = 0; i < getNumOfWks(); i++) {
                if (m.getGrossRequirements().get(i).getLongValue() < 0
                        || m.getPlannedInventory().get(i).getLongValue() < 0
                        || m.getPlannedReceipts().get(i).getLongValue() < 0) {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful",
                            "Sorry. On hand inventory cannot be negative. Please check week" + (i + 1) + " for material " + m.getBom().getMaterial().getName()));
                    return null;
                }
            }
        }
        // persist mrp into database
        if (mrpSession.persistNewMrps(newMRPs, workspaceManagerBean.getCurrentStaff().getId())) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(NEW_MRP);
            return getRedirectPath(Consts.CREATE_MRP_MRP_PATH + "Confirm");
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful",
                    "not able to persist into the database"));
            return null;
        }
    }

    public ArrayList<MRP> getNewMRPs() {
        if (newMRPs == null) {
            ArrayList<MRP> temp = (ArrayList<MRP>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(NEW_MRP);
            if (temp != null) {
                newMRPs = temp;
                // set gross req and onhand inventory
                for (MRP m : newMRPs) {
                    weeklyDemands = mrpSession.getWeeklyDemands(m.getStartPeriodYear(), m.getStartPeriodMonth());
                    if (weeklyDemands.get(0).get(4) == null) {
                        numOfWks = 4;
                    } else {
                        numOfWks = 5;
                    }
                    for (int i = 0; i < numOfWks; i++) {
                        m.getGrossRequirements().add(new IndexNValue(i, new Long(0)));
                        m.getPlannedInventory().add(new IndexNValue(i, new Long(0)));
                        m.getPlannedReceipts().add(new IndexNValue(i, new Long(0)));
                    }
                    for (int dayId = 0; dayId < numOfWks; dayId++) {
                        // for gross req
                        double sum = 0;

                        for (int i = 0; i < 4; i++) {
                            double kg;
                            if (i == 0 || i == 1) {
                                kg = 12;
                            } else {
                                kg = 13.32;
                            }
                            if (weeklyDemands.get(i).get(dayId) == null) {
                                sum += 0;
                            } else {
                                sum += weeklyDemands.get(i).get(dayId) * kg * m.getBom().getMaterialRequirements().get(i).getValue();
                            }
                        }
                        Long result = Math.round(Math.ceil(sum));
                        m.getGrossRequirements().get(dayId).setLongValue(result);
                    }
                    // for onhand inventory
                    Long prev = mrpSession.getPlannedInventoryForPrevWeek(m.getStartPeriodYear(), m.getStartPeriodMonth(), m.getBom());
                    plannedInventory.set(0, prev);
                    for (int i = 0; i < numOfWks; i++) {
                        // set planned inventory
                        Long diff = plannedInventory.get(i) + plannedReceiptsList.get(i) - m.getGrossRequirements().get(i).getLongValue();
                        m.getPlannedInventory().get(i).setLongValue(diff);
                        plannedInventory.set(i + 1, diff);
                    }
                }
            }
        }
        return newMRPs;
    }

    public void setNewMRPs(ArrayList<MRP> newMRPs) {
        this.newMRPs = newMRPs;
    }

    public MRP getSelectedMRP() {

        return selectedMRP;
    }

    public void setSelectedMRP(MRP selectedMRP) {
        this.selectedMRP = selectedMRP;
    }

    // for dialog
    public Double returnKG(Long l, int productId) {
        if (l == null) {
            return null;
        }
        if (selectedMRP == null) {
            return 0.0;
        }
        switch (productId) {
            case 0:
                return 12.0 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 1:
                return 12.0 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 2:
                return 13.32 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 3:
                return 13.32 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            default:
                return 0.0;
        }
    }

    public Long returnGrossRequirements(int dayId) {
        if (dayId + 1 > numOfWks) {
            return null;
        }
        if (selectedMRP == null) {
            return 0L;
        }
//        double sum = 0;
//        for (int i = 0; i < 4; i++) {
//            double kg;
//            if (i == 0 || i == 1) {
//                kg = 12;
//            } else {
//                kg = 13.32;
//            }
//            sum += weeklyDemands.get(i).get(dayId) * kg * selectedMRP.getBom().getMaterialRequirements().get(i).getValue();
//        }
//        Long result = Math.round(Math.ceil(sum));
//        selectedMRP.getGrossRequirements().get(dayId).setLongValue(result);
//        return result;
        return selectedMRP.getGrossRequirements().get(dayId).getLongValue();
    }

    public ArrayList<Long> getPlannedReceiptsList() {
        if (selectedMRP == null) {
            return plannedReceiptsList;
        }
        for (int i = 0; i < numOfWks; i++) {
            plannedReceiptsList.set(i, selectedMRP.getPlannedReceipts().get(i).getLongValue());
        }
        return plannedReceiptsList;
    }

    public ArrayList<LongHolder> getInputLotNumbers() {
        return inputLotNumbers;
    }

    public ArrayList<Long> getPlannedInventory() {
        if (selectedMRP == null) {
            return plannedInventory;
        }
        Long prev = mrpSession.getPlannedInventoryForPrevWeek(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth(), selectedMRP.getBom());
        plannedInventory.set(0, prev);
//        for (int i = 0; i < 5; i++) {
//            // set planned inventory
//            Long diff = plannedInventory.get(i) + plannedReceiptsList.get(i) - selectedMRP.getGrossRequirements().get(i).getLongValue();
//            selectedMRP.getPlannedInventory().get(i).setLongValue(diff);
//            plannedInventory.set(i + 1, diff);
//        }
        for (int i = 1; i < numOfWks + 1; i++) {
            plannedInventory.set(i, selectedMRP.getPlannedInventory().get(i - 1).getLongValue());
        }
        return plannedInventory;
    }

    public void setInputLotNumbers(ArrayList<LongHolder> inputLotNumbers) {
        this.inputLotNumbers = inputLotNumbers;
    }

    public void setPlannedReceiptsList(ArrayList<Long> plannedReceiptsList) {
        this.plannedReceiptsList = plannedReceiptsList;
    }

    public String selectMrpId(MRP mrp) {
        setSelectedMRP(mrp);
        inputLotNumbers = new ArrayList<LongHolder>();
        for (int i = 0; i < numOfWks; i++) {
            LongHolder temp = new LongHolder();
            temp.setL(selectedMRP.getPlannedReceipts().get(i).getLongValue() / selectedMRP.getBom().getMaterial().getLotSize());
            inputLotNumbers.add(temp);
            plannedReceiptsList.set(i, selectedMRP.getPlannedReceipts().get(i).getLongValue());
        }
        return null;
    }

    public ArrayList<ArrayList<Long>> getWeeklyDemands() {
        if (selectedMRP == null) {
            return weeklyDemands;
        }
        weeklyDemands = mrpSession.getWeeklyDemands(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth());
        return weeklyDemands;
    }

    public Integer getNumOfWks() {
        if (numOfWks == null) {
            if (getWeeklyDemands().get(0).get(4) == null) {
                numOfWks = 4;
            } else {
                numOfWks = 5;
            }
        }
        return numOfWks;
    }

    // for rendering cell when < 5
    public boolean renderCell(Integer dayId) {
        if (dayId + 1 <= getNumOfWks()) {
            return true;
        } else {
            return false;
        }
    }

    // for general purpose
    public ArrayList<Integer> getCounterForProduct() {
        return counterForProduct;
    }

    public ArrayList<Integer> getCounterForWeek() {
        return counterForWeek;
    }

    private Long roundToLong(Double d) {
        return Math.round(d);
    }

    private String getRedirectPath(String path) {
        return path + "?faces-redirect=true";
    }

    public String getMonthEnglish(int i) {
        switch (i) {
            case 1:
                return Consts.JAN;
            case 2:
                return Consts.FEB;
            case 3:
                return Consts.MAR;
            case 4:
                return Consts.APR;
            case 5:
                return Consts.MAY;
            case 6:
                return Consts.JUN;
            case 7:
                return Consts.JUL;
            case 8:
                return Consts.AUG;
            case 9:
                return Consts.SEP;
            case 10:
                return Consts.OCT;
            case 11:
                return Consts.NOV;
            case 12:
                return Consts.DEC;
            default:
                return "";
        }
    }

    private int getMonthIndex(String label) {
        if (label.equals(Consts.JAN)) {
            return 1;
        } else if (label.equals(Consts.FEB)) {
            return 2;
        } else if (label.equals(Consts.MAR)) {
            return 3;
        } else if (label.equals(Consts.APR)) {
            return 4;
        } else if (label.equals(Consts.MAY)) {
            return 5;
        } else if (label.equals(Consts.JUN)) {
            return 6;
        } else if (label.equals(Consts.JUL)) {
            return 7;
        } else if (label.equals(Consts.AUG)) {
            return 8;
        } else if (label.equals(Consts.SEP)) {
            return 9;
        } else if (label.equals(Consts.OCT)) {
            return 10;
        } else if (label.equals(Consts.NOV)) {
            return 11;
        } else if (label.equals(Consts.DEC)) {
            return 12;
        } else {
            return 0;
        }
    }

    public String returnProductAlpha(Integer i) {
        if (i.intValue() == 0) {
            //return Consts.ProductType.BAR_A.getText();
            return "A";
        } else if (i.intValue() == 1) {
            //return Consts.ProductType.BAR_B.getText();
            return "B";
        } else if (i.intValue() == 2) {
            //return Consts.ProductType.BAR_C.getText();
            return "C";
        } else if (i.intValue() == 3) {
            //return Consts.ProductType.BAR_D.getText();
            return "D";
        } else {
            return "";
        }
    }

    public String returnProductName(Integer i) {
        if (i.intValue() == 0) {
            return Consts.ProductType.BAR_A.getText();
        } else if (i.intValue() == 1) {
            return Consts.ProductType.BAR_B.getText();
        } else if (i.intValue() == 2) {
            return Consts.ProductType.BAR_C.getText();
        } else if (i.intValue() == 3) {
            return Consts.ProductType.BAR_D.getText();
        } else {
            return "";
        }
    }

    public String returnFormattedDay(Timestamp t) {
        if (t == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
        return sdf.format(t);
    }

    public WorkspaceManagerBean getWorkspaceManagerBean() {
        return workspaceManagerBean;
    }

    public void setWorkspaceManagerBean(WorkspaceManagerBean workspaceManagerBean) {
        this.workspaceManagerBean = workspaceManagerBean;
    }
}
